$(function() {
    $(".loadingMarsk").css({
        "width":window.screen.availWidth,
        "height":window.screen.availHeight,
    });
    new FastClick(document.body);
    //获取验证码
    var flag = false;
    $(".getCodeBtn").on("click",function() {
        if(flag) return false;
        var mobile = $(".mobile").val();
        if(!mobile) {
            tips("请输入手机号码");
            return false;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/gettelcode",
            // url: domain.online + "/captcha",
            data: {
                mobile:mobile
            },
            beforeSend: function () {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
            },
            success: function (response) {
                console.log(response);
                if(response.data.rtnCode===200) {
                // if(response.rtnCode===200) {
                    $(".loadingMarsk").css("display","none");
                    var counter = 90;
                    var timer = setInterval(function() {
                        counter--;
                        if(counter===0) {
                            $(".getCodeBtn").html("获取短息验证码");
                            clearInterval(timer);
                        }else {
                            $(".getCodeBtn").html(counter + "s");
                            flag = true;
                        }
                    },1000);
                }
            }
        });
    });
    //找回密码
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    $(".submitBtn").on("click",function(){
        var userName = $(".userName").val();
        if(!userName) {
            tips("请输入账号");
            return false;            
        }
        var mobile = $(".mobile").val();
        if(!mobile) {
            tips("请输入手机号码");
            return false;
        }
        var code = parseInt($(".code").val());
        if(!code) {
            tips("请输入验证码");
            return false;
        }
        var password = $(".password").val();
        if(!password) {
            tips("请输入新密码");
            return false;
        }
        if(password.length<6||password.length>20) {
            tips("密码长度为6-20位");
            return false;
        }
        $.ajax({
            type: "post",
            url: domain.test + "/resetpsd",
            // url: domain.online + "/resetPwd",
            data: {
                userName:userName,
                password:password,
                mobile:mobile,
                captcha:1234,
                os:"iOS",
                pushId:"sir555"
            },
            beforeSend: function () {
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
            },
            success: function (response) {
                if(response.data.rtnCode===200) {
                // if(response.rtnCode===200) {
                    $(".loadingMarsk").css("display","none");
                    var userData = response.data.data;
                    localStorage.token = userData.token;
                    localStorage.userHeadPic = userData.pic;
                    localStorage.userName = userData.userName;
                    localStorage.nickname = userData.nickname;
                    localStorage.userMobile = userData.mobile
                    localStorage.gender = userData.sex;
                    localStorage.birthday = userData.birthday;
                    tips("修改成功，正在为你跳转！");
                    setTimeout(function() {
                        location.href = "../me/me.html";
                    },1000);
                }
                if(response.data.rtnCode===400) {
                // if(response.rtnCode===400) {
                    $(".loadingMarsk").css("display","none");
                    tips(response.data.msg);
                    // tips(response.msg);
                }
            }
        });
    }); 
});